<template>
  <div class="app-container">
    <div class="search-bar">
      <span style="width:100px">用户手机号</span>
      <el-input v-model="phone" placeholder="请输入关键词" />
      <el-button
        style="margin-left: 10px"
        type="primary"
        @click="searchByBookName"
      ><i class="el-icon-search" />搜索</el-button>
      <el-button
        type="success"
      ><i
        class="el-icon-refresh"
        @click="freshTable"
      /></el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      stripe
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="120">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="用户ID" width="160">
        <template slot-scope="scope">
          {{ scope.row.orderInfo.userId }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="用户手机号" width="160">
        <template slot-scope="scope">
          {{ scope.row.orderInfo.phone }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="地址">
        <template slot-scope="scope">
          {{ scope.row.orderInfo.address }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间">
        <template slot-scope="scope">
          {{ scope.row.orderInfo.createAt }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="总金额">
        <template slot-scope="scope">
          {{ scope.row.orderInfo.total }}
        </template>
      </el-table-column>
      <el-table-column type="expand">
        <template slot-scope="scope">
          <h2>订单商品记录</h2>
          <el-table
            :data="scope.row.shopInfo"
            border
            fit
            highlight-current-row
            stripe
          >
            <!-- 书籍编号 -->
            <el-table-column label="书籍编号">
              <template slot-scope="props">
                {{ props.row.bookId }}
              </template>
            </el-table-column>
            <!-- 创建时间 -->
            <el-table-column label="创建时间">
              <template slot-scope="props">
                {{ props.row.createAt }}
              </template>
            </el-table-column>
            <!-- 购买数量 -->
            <el-table-column label="书籍数量">
              <template slot-scope="props">
                {{ props.row.num }}
              </template>
            </el-table-column>
            <el-table-column label="总金额">
              <template slot-scope="props">
                {{ props.row.total }}
              </template>
            </el-table-column>
          </el-table>
          <h2>用户基本信息</h2>
          <el-form :model="scope.row.userInfo" inline>
            <el-form-item label="用户名">
              <el-tag>{{ scope.row.userInfo.nickName }}</el-tag>
            </el-form-item>
            <el-form-item label="地址">
              <el-tag>{{ scope.row.userInfo.address }}</el-tag>
            </el-form-item>
            <el-form-item label="电话">
              <el-tag>{{ scope.row.userInfo.phone }}</el-tag>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        :current-page="pagination.currentPage"
        :page-sizes="[10, 25, 20, 25]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { getOrderList } from '@/api/order'
export default {
  data: function() {
    return {
      listLoading: false,
      list: [],
      total: 0,
      pagination: {
        currentPage: 1,
        pageSize: 10
      },
      phone: ''
    }
  },
  created() {
    this.getTableData()
  },
  methods: {
    getTableData() {
      this.listLoading = true
      getOrderList(this.phone, this.pagination.currentPage, this.pagination.pageSize).then((response) => {
        console.log(response.data)
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    // 分页变化
    handleSizeChange(pageSize) {
      this.pagination.pageSize = pageSize
      this.getTableData()
    },

    // 每页数量发生变化
    handleCurrentChange(currentPage) {
      this.pagination.currentPage = currentPage
      this.getTableData()
    },
    searchByBookName() {
      this.getTableData()
    },
    freshTable() {
      this.phone = ''
      this.pagination.currentPage = 1
      this.pagination.pageSize = 10
      this.getTableData()
    },
    // 删除用户
    handleDeleteUser(i, user) {
      console.log(user)
      this.$confirm('确认删除该用户嘛?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消'
      }).then(() => {
        console.log('删除书籍id:' + user.id)
      })
    },
    // 更新用户信息
    handleUpdateUser() {

    }
  }

}
</script>

<style lang="scss" scoped>
.search-bar {
  display: flex;
  justify-content: center;
  align-items: center;
  padding-bottom: 5px;
}
.pagination {
  padding-top: 5px;
  display: flex;
  justify-content: right;
}
</style>
